package com.gzz.demo.multithread.pool;


import java.util.concurrent.*;


public class ExecutorsDemo {

    /**
     * 接收和分发任务，当阻塞队列慢了之后，应该拒绝任务并抛出异常。
     */
    private ExecutorService subReactorPool = new ThreadPoolExecutor(3, 4, 10
            , TimeUnit.SECONDS, new ArrayBlockingQueue<>(200));

    /**
     * 工作线程池，当阻塞队列满了之后，应该阻塞subReactorPool的线程
     */
    private ExecutorService workPool = new ThreadPoolExecutor(10, 20, 10, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(600), (r, executor) -> {
                try {
                    executor.getQueue().put(r);   //自定义线程池拒绝策略
                } catch (InterruptedException e) {
                    //log.info("异常了", e.getMessage());
                }
            });
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        // 线程工厂，自定义线程名
        ThreadFactory namedFactory = null;//= new ThreadFactoryBuilder().setNameFormat("doc-detail-contact-%d").build();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 4, 60L
                , TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(10), namedFactory);
    }
}
